package com.shroo8.footballbloglib.dao.util;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.StoredProcedure;
import org.springframework.stereotype.Repository;

@Repository(value="daoUtil")
public class DaoUtilImpl implements DaoUtil {

	private static final Logger logger = LoggerFactory.getLogger(DaoUtilImpl.class);

	
	public Map<String, Object> spExecute(DataSource dataSource, String sql, List<SqlParameter> sqlParameters,
            final Map<String, Object> inParams) throws DataAccessException {
        StoredProcedure storedProcedure = new StoredProcedure(){	
        };
        storedProcedure.setDataSource(dataSource);
        storedProcedure.setSql(sql);
        
        SqlParameter param;
                
    	logger.info("outside loop, param list size is: "+sqlParameters.size());
        
        for (Iterator<SqlParameter> i = sqlParameters.iterator(); i.hasNext();) {
        	param = (SqlParameter) i.next();
        	logger.info("parameter name: "+param.getName());
        	logger.info("parameter type: "+param.getSqlType());
        	logger.info("parameter scale: "+param.getClass().getName());
        	
            storedProcedure.declareParameter(param);
        }
        storedProcedure.compile();
        logger.info("sql procedure call: "+storedProcedure.getCallString());
        logger.info("sql : "+storedProcedure.getSql());
        
        
        return storedProcedure.execute(inParams);
    }
}
